package com.sinopec.handdevice.view

import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.alibaba.android.arouter.facade.annotation.Route
import com.gyf.immersionbar.ImmersionBar
import com.jakewharton.rxbinding3.view.clicks
import com.sinopec.basemodule.adapter.CommonAdapter
import com.sinopec.basemodule.adapter.CustomViewHolder
import com.sinopec.basemodule.base.BaseActivity
import com.sinopec.basemodule.base.CustomToolbar
import com.sinopec.basemodule.dialog.CommonTipsPopupWindow
import com.sinopec.basemodule.model.StockDataItemModel
import com.sinopec.basemodule.model.StockInListModel
import com.sinopec.basemodule.launchcontract.UHFInResultLaunch
import com.sinopec.basemodule.utils.ARouterManagerUtils
import com.sinopec.handdevice.R
import com.sinopec.handdevice.contract.StockInDetailContract
import com.sinopec.handdevice.presenter.StockInDetailPresenter
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_stock_in_detail.*
import java.util.concurrent.TimeUnit

/**
 * @author lwj
 * @ClassName: stockInDetailActivity
 * @Description: 入库详情
 */
@Route(path = ARouterManagerUtils.GOTO_MAIN_STOCK_IN_DETAIL_ACTIVITY)
class StockInDetailActivity : BaseActivity<StockInDetailPresenter>(), StockInDetailContract.View {
    //提示语弹窗
    private lateinit var commonTipsPopupWindow: CommonTipsPopupWindow

    //data数据集合
    private var stockDataItemModels: ArrayList<StockDataItemModel> = arrayListOf()
    private var stockDataItemAdapter: CommonAdapter<StockDataItemModel>? = null

    private val mStockInListModel by lazy {
        intent.getParcelableExtra("stockInListModel") ?: StockInListModel()
    }

    /**
     * 入库扫码启动器
     */
    private val uhfInLauncher by lazy {
        registerForActivityResult(UHFInResultLaunch) {
            if (it != null && it) {
                finish()
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mPresenter = StockInDetailPresenter(this)
        stockDataItemModels.add(
            StockDataItemModel(
                "库位名称",
                mStockInListModel.locationName.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "物料名称",
                mStockInListModel.mtrlName.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "机器码",
                mStockInListModel.machineCode.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "预计总重量（吨）",
                mStockInListModel.expectedPalletQuantity.toString()
            )
        )

        stockDataItemModels.add(
            StockDataItemModel(
                "任务优先级",
                mStockInListModel.taskPriority.toString()
            )
        )
        //任务单号
        tv_taskNo.text = "任务单号：" + mStockInListModel.taskNo
        stockDataItemAdapter = object : CommonAdapter<StockDataItemModel>(mContext,
            R.layout.activity_stock_in_detail_data_item,
            stockDataItemModels) {
            override fun convert(
                holder: CustomViewHolder,
                model: StockDataItemModel,
                position: Int,
            ) {
                val itemView = holder.itemView
                //类别名
                val tv_typeName = holder.getView<TextView>(R.id.tv_typeName)
                tv_typeName.text = model.typeName
                //类别值
                val tv_typeValue = holder.getView<TextView>(R.id.tv_typeValue)
                tv_typeValue.text = model.typeValue
                if (position % 2 == 0) {
                    itemView.setBackgroundColor(Color.parseColor("#ffffff"))
                } else {
                    itemView.setBackgroundColor(Color.parseColor("#F8F8F8"))
                }
            }
        }
        rv_list_data.adapter = stockDataItemAdapter
    }

    override fun getContentView(): Int {
        return R.layout.activity_stock_in_detail
    }

    override fun setStatusBar() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
    }

    override fun configToolbar(rToolbar: CustomToolbar) {
        //配置标题
        rToolbar.setTitle("入库详情")
    }

    override fun onResume() {
        super.onResume()

    }

    override fun initEvent() {
        super.initEvent()

        /**
         * 入库扫码启动器
         */
        uhfInLauncher

        /**
         * 确认
         */
        addDisposable(tv_confirm
            .clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                showCommonTipsPopupWindow(
                    0,
                    "提示",
                    "是否确认出库完成?",
                    "确认",
                    "取消"
                )
            })

        /**
         * 扫码
         */
        addDisposable(ll_scan
            .clicks()
            .throttleFirst(1, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                uhfInLauncher.launch(mStockInListModel)
            })
    }

    /**
     * @author lwj
     * @Description: 二次确认通用弹窗
     */
    private fun showCommonTipsPopupWindow(
        @DrawableRes resId: Int,
        title: String,
        content: String,
        confirmText: String,
        cancelText: String,
    ) {
        commonTipsPopupWindow = CommonTipsPopupWindow.create(
            mContext,
            resId,
            title,
            content,
            confirmText,
            cancelText,
            object : CommonTipsPopupWindow.OnSelectListener {
                override fun onConfirm() {
                    mStockInListModel.taskRecordId?.let {
                        mPresenter?.confirmStockInOrderFinish(it)
                    }
                    commonTipsPopupWindow.dismiss()
                }

                override fun onCancel() {
                    commonTipsPopupWindow.dismiss()
                }
            }).setAnchorView(mContext.rootView)
            .apply()
        commonTipsPopupWindow.showAtLocation(mContext.rootView, Gravity.CENTER, 0, 0)
    }
}